package com.fdb.efp.nls.service.impl.sx;

import com.fdb.efp.nls.service.facade.NlsQueueSoltComnService;
import com.fdb.efp.nls.service.impl.sx.zxruleforbd.BdZxRuleInterface;
import com.fdb.efp.nls.service.vo.queue.NlsProcessBizVO;
import com.fdb.efp.nls.service.vo.sx.NlsCreditInfoVO;
import com.fdb.basic.framework.core.exception.BizException;
import com.fdb.cdp.pboc.service.facade.BdZxInfoService;
import com.fdb.cdp.pboc.service.vo.BdZxInfoVO;
import com.fdb.efp.esb.common.constant.MsLoanConstant;
import com.fdb.efp.nls.common.constant.MYDAdaptedSoltEnum;
import com.fdb.efp.rule.common.enumeration.Rule;
import com.fdb.efp.rule.service.facade.RuleCallService;
import com.fdb.efp.rule.service.vo.ReqBdCreditRuleVo;
import com.fdb.efp.rule.service.vo.ReqRuleVo;
import com.fdb.efp.rule.service.vo.RespRuleVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 百度征信插槽 百度82个字段新增需求: <br>
 * <p>
 * 0.1:caoxin:2019/7/11 9:13:  <br>
 * @author caoxin
 * @version 0.1
 * @date 2019/7/11 9:13
 * @since 2019/7/11 9:13
 */
@Service("creditBdZxRuleService")
public class CreditBdZxRuleServiceImpl extends CreditSoltServiceImpl{

    @Override
    protected void doExecute(NlsProcessBizVO nlsProcessBizVo, NlsCreditInfoVO nlsCreditInfoVO) throws Exception {
        logger.info("======>百度征信规则开始<====== 授信申请流水号为:"+nlsCreditInfoVO.getLmtApplySeq());
        try {
            // 获取前一个插槽名称 用于判断该插槽是否执行
            String soltName = nlsProcessBizVo.getSoltName3();
            // 获取当前插槽代码
            String soltCode = nlsCreditInfoVO.getSoltCode();
            // 获取授信申请流水号
            String lmtApplySeq = nlsCreditInfoVO.getLmtApplySeq();
            // 获取产品编码
            String prdId = nlsCreditInfoVO.getPrdId();
            // 获取上一个规则执行代码 如果已经执行 soltApplyPboc 征信服务 则该插槽不执行
            boolean canExecue = StringUtils.equals(MYDAdaptedSoltEnum.SOLT_APPLY_PBOC.getSoltName(), soltName);
            if (canExecue){
                return;
            }
            // 通过征信规则数据
            BdZxInfoVO pbocData = getPbocData(nlsCreditInfoVO);
            // 判断是否为空
            if (Objects.isNull(pbocData)){
                throw new BizException("【百度征信规则插槽】 获取百度授信接口中的征信规则失败!!!");
            }
            // 创建规则引擎执行类
            ReqBdCreditRuleVo param = new ReqBdCreditRuleVo();
            for (BdZxRuleInterface rule : bdZxRules.values()) {
                rule.executeRule(param,pbocData);
            }
            // 创建规则类
            ReqRuleVo reqRuleVo = new ReqRuleVo();
            reqRuleVo.setAppNo(nlsCreditInfoVO.getLmtApplySeq());
            reqRuleVo.setPrdCode(nlsCreditInfoVO.getPrdCode());
            reqRuleVo.setReqBdCreditRuleVo(param);

            // 批量执行规则
            List<RespRuleVO> list = ruleService.executeBatchRule(Rule.RuleType.BD_CREDIT_RULE, reqRuleVo);
            nlsQueueSoltComnService.ruleResultDeal(list,lmtApplySeq,prdId,soltCode);
        } catch (Exception e) {
            e.printStackTrace();
            nlsCreditInfoVO.setBdReasonCode(MsLoanConstant.ReasonCodeMap.get(zxRule));
            nlsCreditInfoVO.setBdReasonMsg(MsLoanConstant.ReasonMsgMap.get(zxRule));
            nlsProcessBizVo.setExceptionFlag(Boolean.TRUE);
            nlsProcessBizVo.setRefuseMsg(e.getMessage());
            nlsProcessBizVo.setExceptionMsg(e);
        }
    }

    /**
     * 获取百度接口征信数据: <br>
     * 0.1:caoxin:2019/7/11 9:39: TODO: <br>
     * @author caoxin
     * @return
     * @since 2019/7/11 9:39
     * @date 2019/7/11 9:39
     * @version 0.1
     */
    private BdZxInfoVO getPbocData(NlsCreditInfoVO nlsCreditInfoVO) {
        // 设置授信申请流水号
        BdZxInfoVO bdZxInfoVO = new BdZxInfoVO();
        bdZxInfoVO.setLmtApplySeq(nlsCreditInfoVO.getLmtApplySeq());
        return bdZxInfoService.queryByPk(bdZxInfoVO);
    }
    /**
     * 日志工具
     */
    private static final Logger logger = LoggerFactory.getLogger(CreditBdZxRuleServiceImpl.class);
    /**
     * 征信规则
     */
    @Autowired
    Map<String, BdZxRuleInterface> bdZxRules;
    /**
     * 征信获取参数
     */
    @Autowired
    @Qualifier("bdZxInfoService")
    private BdZxInfoService bdZxInfoService;
    /**
     * 规则执行辅助类
     */
    @Autowired
    @Qualifier("nlsQueueSoltComnService")
    private NlsQueueSoltComnService nlsQueueSoltComnService;
    /**
     * 规则执行服务类
     */
    @Autowired
    @Qualifier("ruleCallService")
    private RuleCallService ruleService;

    private static final String zxRule = "CcrcRule";
}
